package org.csu.user.controller;


import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.csu.exception.UnauthorizedException;
import org.csu.user.entity.dto.UserDTO;
import org.csu.user.entity.po.Users;
import org.csu.user.service.IUsersService;
import org.csu.utils.Result;
import org.csu.utils.UserContext;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2025-07-08
 */
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
@Slf4j
public class UsersController {
    private final IUsersService usersService;


    @PostMapping("/login")
    public Result<Map<String,Object>> login(@RequestBody UserDTO userDTO){
        log.info("进行登陆操作：{}",userDTO.toString());
        Map<String, Object> map = usersService.login(userDTO);
        if(map != null){
            return Result.success(map);
        }else{
            return Result.error("用户名或密码错误");
        }
    }

    @PostMapping("/register")
    public Result<Users> register(@RequestBody Users user){
        log.info("进行注册操作：{}",user.toString());
        Users u = usersService.register(user);
        if(u != null){
            return Result.success(u);
        }else{
            return Result.error("注册失败");
        }
    }

    @GetMapping("/send-verification-code")
    public Result<String> sendEmailCode(@RequestParam("email") String email){
        log.info("进行发送邮箱验证码操作：{}",email);
        usersService.sendEmailCode(email);
        return Result.success("发送成功");
    }

    @PostMapping("/login/email")
    public Result<Map<String,Object>> loginByEmail(@RequestBody Map<String,Object> userDTO){
        log.info("进行邮箱登陆操作：{}",userDTO.toString());
        Map<String, Object> map = usersService.loginByEmail(userDTO);
        if(map != null){
            return Result.success(map);
        }else{
            return Result.error("用户名或密码错误");
        }
    }

    @GetMapping("/refresh-token")
    public Result<String> refreshToken(HttpServletRequest request){
        String refreshToken = request.getHeader("RefreshToken");
        return Result.success(usersService.refreshToken(refreshToken));
    }

    @GetMapping("/logout")
    public Result<String> logout(){
        long userId= UserContext.getUser();
        usersService.logout(userId);
        return Result.success("退出成功");
    }

    @PostMapping("/profile")
    public Result<Users> changeProfile(@RequestBody Users user){
        long userId=UserContext.getUser();
        if(userId==0){
            throw new UnauthorizedException("未登录");
        }
        user.setUid(userId);
        return Result.success(usersService.changeProfile(user));
    }

    @PostMapping("/password")
    public Result<String> changePassword(@RequestBody Map<String,Object> dto){
        long userId=UserContext.getUser();
        if(userId==0){
            throw new UnauthorizedException("未登录");
        }
        usersService.changePassword(dto,userId);
        return Result.success();
    }

    @PostMapping("/avatar")
    public Result<String> changeAvatar(@RequestBody MultipartFile file){
        long userId=UserContext.getUser();
        if(userId==0){
            throw new UnauthorizedException("未登录");
        }
        usersService.changeAvatar(file,userId);
        return Result.success();
    }
}
